 /*
	@Author: 于辉
	@Time: 2020-11
	@Tittle: rolelist
	@Description: 用户角色管理
*/

 
 var layindex;
 var editInfo,deleteInfo;
 layui.use(['form','layer','jquery',"table"],function(){
 	var table = layui.table;
 	var form = layui.form;
 	var layer = layui.layer;
    //渲染数据列表
	var renderTable = function (param){
		table.render({
			id: 'userID'
		    ,elem: '#list'
            ,even: true //开启隔行背景
		    ,url:getServerPath("GETROLELIST")
		    ,method: 'post' 
		    ,contentType: 'application/json'
		    ,height: 'full-150'
		    ,headers:{SYSTOKEN: getSettings("token")}
		    ,where: {
			    "name": $("#search_input").val(),
				"orderBy": "createTime",
				"sort": "descending"
			} 
		    ,parseData: function(res){ //res 即为原始返回的数据
		    	console.log(res)
			    return {
			      "code": res.statusCode == 200?0:res.statusCode, //解析接口状态
			      "msg": res.message, //解析提示文本
			      "count": res.data.totalCount, //解析数据长度
			      "data": res.data.dataSource //解析数据列表
			    };
			}
		    ,cols: [[
		      {type:'checkbox'}
		      ,{field:'id', width:80, title: 'ID',hide:true, sort: true}
		      ,{field:'name', width:300, title: '角色名称',align:"center"}
		      ,{field:'createTime', width:150, title: '创建时间', sort: true,align:"center",templet:function(row){
		      	return new Date(row.createTime).Format("yyyy-MM-dd");
		      }}
		      ,{field:'remark',  title: '角色备注',align:"center",templet:function(row){
		      	if(row.remark){
		      		return "<div class='align-left'>"+row.remark+"</div>";
		      	}
		      	return "";
		      }}
		      ,{field:'', width:200, title: '操作' ,align:"center" , templet:function(row){
		      	var html = '<button type="button" class="layui-btn layui-btn-sm" onclick="editInfo(\''+JSON.stringify(row).replace(/"/g, '&quot;') +'\')"><i class="layui-icon layui-icon-edit"></i>编辑</button>';
		      	html += '<button type="button" class="layui-btn layui-btn-danger layui-btn-sm" onclick="deleteInfo(\''+row.id+'\',false)"><i class="layui-icon layui-icon-delete"></i>删除</button>';
		      	
		      	return html;
		      }}
		    ]]
		    ,page: true
		});
        $('#page').css('text-align', 'right');  // 分页右对齐
        $('.layui-table-page').css('text-align', 'right');  // 数据表格分页右对齐
	}

    
    renderTable();
	 //搜索
    layui.$("#btn_search").on("click", function () {
        renderTable( );
    })
    //添加用户
    layui.$('#btn_add').on('click', function(){
	    //页面层-自定义
		layindex = layer.open({
		  type: 1,
		  title: "角色信息",
		  closeBtn: 1,
		  shadeClose: true,
		  area: ['603px', '350px'],
		  content: $("#detailInfo")		  
		});
  	});
  	//批量禁用用户
  	layui.$('#btn_enabled').on('click', function(){
  	 	var checkStatus = table.checkStatus('userID'); 
  	 	if(checkStatus.data.length<=0){
  	 		layer.alert("请至少选中一条数据进行操作!",{icon:5});
  	 		return;
  	 	}
  	 	var selectIds = new Array();
  	 	for(var i=0;i<checkStatus.data.length;i++){
  	 		selectIds.push(checkStatus.data[i].userID);
  	 	}
  	 	setUserEnable(selectIds,false);
  	});
  	//批量恢复用户
  	layui.$('#btn_recovery').on('click', function(){
  	 	var checkStatus = table.checkStatus('userID'); 
  	 	if(checkStatus.data.length<=0){
  	 		layer.alert("请至少选中一条数据进行操作!",{icon:5});
  	 		return;
  	 	}
  	 	var selectIds = new Array();
  	 	for(var i=0;i<checkStatus.data.length;i++){
  	 		selectIds.push(checkStatus.data[i].userID);
  	 	}
  	 	setUserEnable(selectIds,true);
  	});
  	
  	//详情页开始
  	form.render();
  	form.verify({
	    name: function(value){
	      if(value.length < 4){
	        return '用户名至少得4个字符啊';
	      }
	      
	    }
	  });

     $("#btn_cancle").click(function () {
         layer.close(layindex);
     })

  	//信息维护页面提交
  	form.on('submit(btn_save)', function(data){
		data = data.field;
	    console.log(data)
		var serverpath = getServerPath("CREATEROLE");
		if($("#id").val() != ''){
			serverpath = getServerPath("UPDATEROLE");
		}
		ajaxSetting({
			url:serverpath,
			data:JSON.stringify(data),
			async:false,
			success:function(res){
				layer.msg("保存成功",{icon:6})
				//执行查询功能
				$("#btn_search").trigger("click");
				setTimeout(function(){
					layer.close(layindex);
				},500)
				
			},
			error:function(res){
				console.log(res)
			}
		})
		return false;
	});
	//编辑用户信息
	editInfo = function (detailInfo){
		detailInfo = JSON.parse(detailInfo);
	    //页面层-自定义
		layindex = layer.open({
		  type: 1,
		  title: "角色信息",
		  closeBtn: 1,
		  shadeClose: true,
		  area: ['603px', '350px'],
		  content: $("#detailInfo"),		
		  success:function(layero,index){
				form.val('info', {
					"id": detailInfo.id
			        ,"name": detailInfo.name // "name": "value"
			        ,"remark":detailInfo.remark
			    });
		  }
		});
	}
	deleteInfo = function (id){
		layer.confirm('是否确定要删除该角色？', {
		  btn: ['删除','取消'] //按钮
		}, function(){
		    ajaxSetting({
			 	url:getServerPath("DELETEROLE"),
				data:{id:id},
				type:'get',
				success:function(res){
					layer.msg("删除成功",{icon:6})
					//执行查询功能
					$("#btn_search").trigger("click");					
				},
				error:function(res){
				}
			})
		}, function(){
		  
		});
	}
 });


